package com.ll.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ll.gulimall.product.entity.ProductAttrValueEntity;
import com.ll.gulimall.product.service.ProductAttrValueService;
import com.ll.gulimall.product.vo.AttrRespVo;
import com.ll.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ll.gulimall.product.service.AttrService;
import com.ll.common.utils.PageUtils;
import com.ll.common.utils.R;


/**
 * 商品属性
 *
 * @author liliang
 * @email liliang@gmail.com
 * @date 2020-07-30 23:16:45
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    // /product/attr/base/listforspu/{spuId}

    // 根据spuId 获取属性
    @GetMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> listforspu = productAttrValueService.listforspu(spuId);

        return R.ok().put("data",listforspu);

    }
    //
    // /product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R listforspu(@RequestBody List<ProductAttrValueEntity> productAttrs, @PathVariable("spuId") Long spuId) {

        productAttrValueService.updateProductAttrValue(spuId,productAttrs);
        return R.ok();

    }


    //  /product/attr/base/list/{catelogId}
    // /product/attr/sale/list/{catelogId}
    //base基本属性  sale消费属性

    /**
     * 获取分类规格参数
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R attrBaseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId")
            Long catelogId,
                          @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryPageBaseList(params, catelogId, type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
